// 贪心算法
// 给定一组价格区间，买卖股票不限次数，求最大利润
// 可以根据贪心的策略进行求解：每次都在低点买，再高点卖，只要价格是上升趋势，就买卖

import java.util.*;
import java.io.*;

public class Agiotage {
    public static Read in = new Read();
    public static void main(String[] args) throws IOException{
        int n = in.nextInt();
        int[] price = new int[n];

        for(int i = 0; i < n; i++){
            price[i] = in.nextInt();
        }

        int ret = 0;
        for(int i = 1; i < n; i++){
            if(price[i] > price[i - 1]){
                ret += (price[i] - price[i - 1]);
            }
        }

        System.out.println(ret);
    }
}

class Read{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    String next() throws IOException{
        while(!st.hasMoreTokens()){
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
}
